/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author anuphabm
 */
public class MainConnMssql {

    public static void main(String[] args) {

// Create a variable for the connection string.
        String connectionUrl = "jdbc:jtds:sqlserver://10.30.3.129:1433;databaseName=stg;user=sa;password=";
        //String connectionUrl = "jdbc:microsoft:sqlserver://10.30.3.129:1433//stg;user=sa;password=";


        // Declare the JDBC objects.
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            // Establish the connection.
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            //CharsetInfo cs = new CharsetInfo(connectionUrl);

            //System.out.println("[" + cs.getCharset()+ "]");

            con = DriverManager.getConnection("jdbc:jtds:sqlserver://10.30.3.129:1433;databaseName=stg",
                    "sa","");

            // Create and execute an SQL statement that returns some data.
            String SQL = "SELECT TOP 10 * FROM anagrafica";
            pstmt = con.prepareStatement(SQL);
            rs = pstmt.executeQuery();

            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                System.out.println(rs.getString(1) + " " + rs.getString(4));
            }
        } // Handle any errors that may have occurred.
        catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (Exception e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (Exception e) {
                }
            }
        }


    }
}
